import { createRouter, createWebHistory } from 'vue-router'
import { AUTHS } from '@/constants'
import {
  House,
  Share,
  Delete,
  Setting,
  Files,
  VideoCamera,
  Microphone,
  Picture,
  Document,
  More
} from '@element-plus/icons-vue'
import { h } from 'vue'

export const routes = [
  {
    path: '/login',
    name: '登录',
    meta: {
      key: 'login'
    },
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/',
    meta: {
      key: 'main'
    },
    component: () => import('@/views/layout/index.vue'),
    children: [
      {
        path: '/',
        redirect: '/main/all'
      },
      {
        path: '/main/:key',
        name: '首页',
        meta: {
          key: 'home',
          auth: AUTHS.ALL,
          icon: () => h(House),
          subChildren: [
            {
              path: '/main/all',
              name: '全部',
              meta: {
                key: 'all',
                icon: () => h(Files)
              }
            },
            {
              path: '/main/video',
              name: '视频',
              meta: {
                key: 'video',
                icon: () => h(VideoCamera)
              }
            },
            {
              path: '/main/audio',
              name: '音频',
              meta: {
                key: 'audio',
                icon: () => h(Microphone)
              }
            },
            {
              path: '/main/image',
              name: '图片',
              meta: {
                key: 'image',
                icon: () => h(Picture)
              }
            },
            {
              path: '/main/doc',
              name: '文档',
              meta: {
                key: 'doc',
                icon: () => h(Document)
              }
            },
            {
              path: '/main/others',
              name: '其他',
              meta: {
                key: 'others',
                icon: () => h(More)
              }
            }
          ]
        },
        component: () => import('@/views/main/index.vue')
      }
      // {
      //   path: '/share',
      //   name: '分享',
      //   meta: {
      //     key: 'share',
      //     auth: AUTHS.ALL,
      //     icon: () => h(Share),
      //     subChildren: [
      //       {
      //         name: '我的分享',
      //         meta: {
      //           key: 'myshare'
      //         }
      //       }
      //     ]
      //   },
      //   component: () => import('@/views/share/index.vue')
      // }
      // {
      //   path: '/delete',
      //   name: '删除',
      //   meta: {
      //     key: 'delete',
      //     auth: AUTHS.ALL,
      //     icon: () => h(Delete),
      //     subChildren: [
      //       {
      //         name: '我的删除',
      //         meta: {
      //           key: 'mydelete'
      //         }
      //       }
      //     ]
      //   },
      //   component: () => import('@/views/delete/index.vue')
      // },
      // {
      //   path: '/setting',
      //   name: '设置',
      //   meta: {
      //     key: 'setting',
      //     auth: AUTHS.ADMIN,
      //     icon: () => h(Setting)
      //   },
      //   component: () => import('@/views/setting/index.vue')
      // }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
